home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example1.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  6KB  |  205 lines

  1. /*
  2. **    example1.c
  3. **
  4. **    This example illustrates how to send two queries to
  5. **    SQL Server in a command batch.  It binds each set
  6. **    of results and prints the rows.
  7. **
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <sybfront.h>
  12. #include <sybdb.h>
  13.  
  14. #define DATELEN     26
  15. #define TYPELEN      2
  16.  
  17. /* Forward declarations of the error handler and message handler. */
  18. int             err_handler();
  19. int             msg_handler();
  20.  
  21. main(argc, argv)
  22. int             argc;
  23. char            *argv[];
  24. {
  25.     DBPROCESS     *dbproc;       /* Our connection with SQL Server. */
  26.     LOGINREC      *login;        /* Our login information. */
  27.  
  28.     /* These are the variables used to store the returning data. */
  29.  
  30.     DBCHAR         crdate[DATELEN+1];
  31.     DBINT          id;
  32.     DBCHAR         name[MAXNAME+1];    /* MAXNAME is defined in
  33.                                         * "sybdb.h" as the maximum
  34.                                         * length for names of database
  35.                                         * objects, such as tables,
  36.                                         * columns, and procedures.
  37.                                         */
  38.     DBCHAR         type[TYPELEN+1];
  39.     RETCODE        result_code;
  40.  
  41.     /* Initialize DB-Library. */
  42.     if (dbinit() == FAIL)
  43.         exit(ERREXIT);
  44.  
  45.     /* Install the user-supplied error-handling and message-handling
  46.      * routines. They are defined at the bottom of this source file.
  47.      */
  48.     dberrhandle(err_handler);
  49.     dbmsghandle(msg_handler);
  50.  
  51.     /*
  52.     ** Get a LOGINREC structure and fill it with the necessary
  53.     ** login information.
  54.     */
  55.  
  56.     login = dblogin();
  57.     DBSETLPWD(login, "server_password");
  58.     DBSETLAPP(login, "example1");
  59.  
  60.     /*
  61.     ** Get a DBPROCESS structure for communicating with SQL Server.
  62.     ** A NULL servername defaults to the server specified by DSQUERY.
  63.     */
  64.  
  65.     dbproc = dbopen(login, NULL);
  66.  
  67.     /*
  68.     ** We are going to retrieve some information, from a table
  69.     ** named "sysobjects", regarding names of system tables and
  70.     ** stored procedures.
  71.     ** We will submit two queries.  The first finds all the rows 
  72.     ** that describe system tables.  The second finds all the rows
  73.     ** that describe stored procedures.  The program will only look
  74.     ** at the first 10 rows that describe stored procedures.
  75.     */
  76.  
  77.     /* First, put the commands into the command buffer. */
  78.  
  79.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  80.     dbcmd(dbproc, " where type = 'S' ");
  81.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  82.     dbcmd(dbproc, " where type = 'P' ");
  83.  
  84.     /*
  85.     ** Sql Server processes the command batch in the following
  86.     ** order:
  87.     **
  88.     ** 1) It will check for syntax errors (i.e., "use database pubs" 
  89.     **    is syntactically incorrect; it should be "use pubs").
  90.     ** 2) The second check is a semantic check (i.e., "select * from 
  91.     **    titels" will be incorrect because the spelling should be 
  92.     **    "titles".)
  93.     ** 3) The third check occurs in the actual execution phase. This 
  94.     **    check involves issues like permissions or memory problems.
  95.     ** 
  96.     ** In the execution phase, dbsqlexec() and dbresults() can return 
  97.     ** the value "SUCCEED", which means there are more commands in the 
  98.     ** batch to process and that that command was successful. A value 
  99.     ** of "FAIL" means that the query failed but there may be more 
  100.     ** commands in the batch to process. A value of "NO_MORE_RESULTS"
  101.     ** means that there are no more commands in the batch to process.
  102.     ** Therefore, the programmer must check the return values after
  103.     ** dbsqlexec() and dbresults(), as illustrated below.
  104.     **
  105.     */
  106.  
  107.     /* Send the commands to SQL Server and start execution. */
  108.     dbsqlexec(dbproc);
  109.  
  110.     /* Process each command until there are no more. */
  111.  
  112.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  113.     {
  114.         if (result_code == SUCCEED)
  115.         {
  116.             /* Bind program variables. */
  117.     
  118.             dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT)0, name);
  119.             dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT)0, type);
  120.             dbbind(dbproc, 3, INTBIND, (DBINT)0, (BYTE *)&id);
  121.             dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT)0, crdate);
  122.  
  123.             /* Print appropriate header for the type of
  124.              * data coming back.
  125.              */
  126.  
  127.             printf("\n %s Objects: \n\n",
  128.             DBCURCMD(dbproc) == 1 ? "System Table": "Procedure");
  129.  
  130.             /* Now print the rows. */
  131.  
  132.             while (dbnextrow(dbproc) != NO_MORE_ROWS)
  133.             {
  134.                 /*
  135.                 ** If this is the 2nd command and
  136.                 ** 10th row, flush the rest of the
  137.                 ** rows for that command.
  138.                 */
  139.  
  140.                 if ((DBCURCMD(dbproc) == 2)
  141.                     && (DBCURROW(dbproc) > 10))
  142.                     continue;
  143.  
  144.                 printf
  145.                     ("%s %s %ld %s\n", name, type, id, crdate);
  146.             }
  147.         }
  148.     }
  149.  
  150.  
  151.     /* Close our connection and exit the program. */
  152.  
  153.     dbexit();
  154.     exit(STDEXIT);
  155. }
  156.  
  157. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  158. DBPROCESS       *dbproc;
  159. int             severity;
  160. int             dberr;
  161. int             oserr;
  162. char            *dberrstr;
  163. char            *oserrstr;
  164. {
  165.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  166.         return(INT_EXIT);
  167.     else 
  168.     {
  169.         printf("DB-Library error:\n\t%s\n", dberrstr);
  170.  
  171.         if (oserr != DBNOERR)
  172.             printf("Operating-system error:\n\t%s\n", oserrstr);
  173.  
  174.         return(INT_CANCEL);
  175.     }
  176. }
  177.  
  178. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  179.                 srvname, procname, line)
  180.  
  181. DBPROCESS       *dbproc;
  182. DBINT           msgno;
  183. int             msgstate;
  184. int             severity;
  185. char            *msgtext;
  186. char            *srvname;
  187. char            *procname;
  188. DBUSMALLINT     line;
  189.  
  190. {
  191.     printf ("Msg %ld, Level %d, State %d\n", 
  192.             msgno, severity, msgstate);
  193.  
  194.     if (strlen(srvname) > 0)
  195.         printf ("Server '%s', ", srvname);
  196.     if (strlen(procname) > 0)
  197.         printf ("Procedure '%s', ", procname);
  198.     if (line > 0)
  199.         printf ("Line %d", line);
  200.  
  201.     printf("\n\t%s\n", msgtext);
  202.  
  203.     return(0);
  204. }
  205.